#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    while (cin >> s)
    {
        unordered_map<char, int> hash;
        for (auto e : s)
            hash[e]++;
        priority_queue<int, vector<int>, greater<>> q;
        for (auto e : hash)
            q.push(e.second);
        int ret = 0;
        while (q.size() > 1)
        {
            int t1 = q.top();
            q.pop();
            int t2 = q.top();
            q.pop();
            ret += t1 + t2;
            q.push(t1 + t2);
        }
        cout << ret << endl;
    }
}